<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
class Api_Model_FriendMapper {
	protected $_db_table;

	public function __construct() {
		$this->_db_table = new Api_Model_DbTable_Friend();
	}

	public function addFriend($data) {

		date_default_timezone_set('Asia/Ho_Chi_Minh');
		$currentDateTime = date('Y-m-d H-i-s');

		$request = array('user_id' => $data['user_id'],
                         'friend_id' => $data['friend_id'],
                         'status' => 'request');

		$affectedRow = $this->_db_table->insert($request);
		if ($affectedRow){
			$this->_db_table->update(array('updated_time' => $currentDateTime),
			array('user_id = ?' => $data['user_id'], 'friend_id = ?' => $data['friend_id']));
		} else{
			throw new Exception('Internal Server Error' ,500);
		}

	}

	public function confirmFriend($data) {
		if ($data['friend_response'] == 'accept'){
			date_default_timezone_set('Asia/Ho_Chi_Minh');
			$currentDateTime = date('Y-m-d H-i-s');

			//update status cho row request
			$where = array('user_id = ?' => $data['friend_id'], 'friend_id = ?' => $data['user_id']);
			$this->_db_table->update(array('status' => 'friend'), $where);

			//confirm
			$response = array('user_id' => $data['user_id'], 'friend_id' => $data['friend_id'], 'status' => 'friend');
			$affectedRow = $this->_db_table->insert($response);
			if ($affectedRow){
				$this->_db_table->update(array('updated_time' => $currentDateTime),
				array('user_id = ?' => $data['user_id'], 'friend_id = ?' => $data['friend_id']));
			} else{
				throw new Exception('Internal Server Error' ,500);
			}
		} elseif ($data['friend_response'] == 'deny') {
			$this->_db_table->delete(array('user_id = ?' => $data['friend_id'], 'friend_id = ?' => $data['user_id']));
		}
	}

	public function unFriend($user_id, $friend_id) {
		$a = $this->_db_table->delete(array('user_id = ?' => $user_id, 'friend_id = ?' => $friend_id));
		$b = $this->_db_table->delete(array('user_id = ?' => $friend_id, 'friend_id = ?' => $user_id));
	}

	public function getAllFriends($user_id) {
		$select = $this->_db_table->select()->setIntegrityCheck(false)
		->from(array('u' => 'users'), array('u.user_id', 'u.username', 'u.fullname', 'u.profile_picture'))
		->joinInner(array('f1' => 'friends'), 'u.user_id = f1.friend_id')
		->joinInner(array('f2' => 'friends'), 'f1.user_id = f2.friend_id and f1.friend_id = f2.user_id')
		->where('f1.user_id = ?', $user_id);
		$result = $this->_db_table->fetchAll($select);


		if(count($result) == 0) {
			throw new Exception(json_encode(array('meta' => 'Not found')),404);
		} else {
			foreach ($result as $row) {
				// $row should be a Zend_Db_Table_Row object
				// cast to array
				$rowArray = $row->toArray();

				//check relation
				$se_friend_a = $this->_db_table->select()->from('friends', array('*'))
				->where('user_id = ?', $user_id)->where('friend_id = ?', $rowArray['user_id']);
				$re_friend_a = $this->_db_table->fetchAll($se_friend_a);

				$se_friend_b = $this->_db_table->select()->from('friends', array('*'))
				->where('user_id = ?', $rowArray['user_id'])->where('friend_id = ?', $user_id);
				$re_friend_b = $this->_db_table->fetchAll($se_friend_b);
				 
				//muatual
				$select_check = $this->_db_table->select()->from('friends', 'user_id')
				->where('friend_id = ?', $rowArray['user_id'])
				->where('status = ?', 'friend');
				$result_check = $this->_db_table->fetchAll($select_check);
				$row_check = $result_check->current();
				$rowArray_check = $row_check->toArray();
				$select_mutual = $this->_db_table->select()->setIntegrityCheck(false)
				->from(array('f' => 'friends'), array('f.user_id'))
				->where('f.user_id IN(?)', $rowArray_check['user_id'])
				->where('friend_id = ?', $user_id)
				->where('status = ?', 'friend');
				$result_mutual = $this->_db_table->fetchAll($select_mutual);
				$count_mutual = count($result_mutual);


				if(count($re_friend_a) == 0 && count($re_friend_b) == 0) {
					$friend = array('user_id' => $rowArray['user_id'],
                                'username' => $rowArray['username'],
                                'fullname' => $rowArray['fullname'],
                                'profile_picture' => $rowArray['profile_picture'],
                                'mutual_count' => $count_mutual,
                                    'relation' => 0);
				} elseif(count($re_friend_a) != 0 && count($re_friend_b) == 0){
					$friend = array('user_id' => $rowArray['user_id'],
                                'username' => $rowArray['username'],
                                'fullname' => $rowArray['fullname'],
                                'profile_picture' => $rowArray['profile_picture'],
                                'mutual_count' => $count_mutual,
                                    'relation' => 2);
				} elseif(count($re_friend_a) == 0 && count($re_friend_b) != 0){
					$friend = array('user_id' => $rowArray['user_id'],
                                'username' => $rowArray['username'],
                                'fullname' => $rowArray['fullname'],
                                'profile_picture' => $rowArray['profile_picture'],
                                'mutual_count' => $count_mutual,
                                    'relation' => 3);
				} elseif(count($re_friend_a) != 0 && count($re_friend_b) != 0){
					$friend = array('user_id' => $rowArray['user_id'],
                                'username' => $rowArray['username'],
                                'fullname' => $rowArray['fullname'],
                                'profile_picture' => $rowArray['profile_picture'],
                                'mutual_count' => $count_mutual,
                                    'relation' => 1);
				}

				$json[] = array('friend' => $friend);

			}
			return $json;
		}
	}

}
